import CONFIG from "./config";
import axios from 'axios';
import { message } from "antd";
import { removeToken } from "./index";
import qs from 'qs'
const { basicProxy, appAddr, helpAddr, downloadAddr } = CONFIG;

export default function axiosMethod({
    type = "get",
    baseUrl = "",  //接口部署地址
    url = "",      //接口相对地址
    body = {},
    header = {},
    isNoTimeout = false,
    showError = true,
    isBackAll = false, //是否返回接口返回的完整数据
    callback = () => { },  //接口请求成功后用于接收响应数据的函数
    onUploadProgress = () => { },  //对上传事件进行监听，接收event对象，可以用于实现显示上传进度
    onDownloadProgress = () => { }  //对下载事件进行监听，接收event对象，可以用于实现显示下载进度
}) {
    let query = {
        baseURL: baseUrl ? baseUrl : basicProxy,
        method: type,
        url,
        timeout: isNoTimeout ? 100000 : 8000,
        responseType: 'json',
        onUploadProgress,
        onDownloadProgress,
        headers: {
            Authorization: `${sessionStorage.getItem("token")}`,
            ...header
        }
    }
    if (type === "get") {
        query.params = body;
    } else {
        query.data = body;
    }

    if (type === 'put' || type === 'PUT') {
        query.url += `?${qs.stringify(body)}`
    }
    typeof window.changeAppLoad === "function" &&
        body.load &&
        window.changeAppLoad(true);
    axios(query)
        .then(res => {
            typeof window.changeAppLoad === "function" &&
                body.load &&
                window.changeAppLoad(false);
            if (res.status === 200) {
                let result = res.data;
                if (result.StatusCode === 200 || result.code === "200") {
                    if (isBackAll) {
                        callback(result);
                    } else {
                        callback(result.Data || result.data);
                    }
                } else {
                    if (isBackAll) {
                        if (showError) message.warn(result.msg)
                    } else {
                        handleStatusCode(result.StatusCode || result.code, result.msg || result.Msg, showError);
                    }
                }
            } else {
                handleStatusCode(res.status, url, showError);
            }
        })
        .catch(err => {
            typeof window.changeAppLoad === "function" &&
                body.load &&
                window.changeAppLoad(false);
            console.log(err);
            if (typeof err === "object" && err.response) {
                handleStatusCode(err.response.status, url, showError);
            }
            if (typeof err === "object" && err.request) {
                callback({});
            }
        })
}

//对返回状态进行特殊处理，例如401， 403为掉线
const handleStatusCode = (data, msg, showError = true) => {
    switch (data.toString()) {
        case "400":
        case "401":
        case "403":
            if (showError) message.warn(msg || "你已掉线，请重新登录");
            setTimeout(() => {
                if (sessionStorage.getItem("baseUrl")) {
                    if (process.env.NODE_ENV === 'production') {
                        // window.location.href = sessionStorage.getItem("baseUrl") + `/UserMgr/Login/Login.aspx?lg_sysid=264&lg_preurl=${encodeURIComponent(removeToken(window.location.href))}`;
                    }
                }
            }, 1000);
            break;
        case "500":
            if (showError) message.error(msg + "接口调用失败");
            break;
        default:
            if (showError) message.warn(msg);
            break;
    }
}

